package com.yuantu.judiciary.controller;

import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.wechat.pay.java.service.refund.model.Refund;
import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.dto.RefundOrderDTO;
import com.yuantu.judiciary.model.form.*;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.IApiWxPayService;
import com.yuantu.judiciary.service.IVipOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 套餐订单模块
 *
 * @author syw
 * @since 2023-08-14
 **/
@RestController
@RequestMapping(value = "/order")
@Api(tags = "套餐订单模块")
@RequiredArgsConstructor
public class VipOrderController extends BaseController {

    private final IVipOrderService orderService;

    private final IApiWxPayService wxPayService;


    @ApiOperation(value = "测试查询")
    @PostMapping("/test/query")
    public void testQuery(@RequestParam Long orderId) {
        orderService.testQuery(orderId);
    }


    @ApiOperation(value = "创建订单")
    @PostMapping("/add")
    public Response<AddOrderVO> add(@RequestBody @Validated AddOrderForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.add(accountId, form));
    }

    @ApiOperation(value = "获取下单参数")
    @GetMapping("/get/param")
    public Response<PrepayWithRequestPaymentResponse> getParam(@RequestParam Long id) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.getParam(accountId, id));
    }

    @ApiOperation(value = "回调接口")
    @PostMapping("/notify")
    public Response callOrder(HttpServletRequest request) {
        Map<String, Object> map = orderService.callOrder(request);
        return new Response(0, map.get("code"), map.get("message").toString());
    }


    @ApiOperation(value = "创建分享订单")
    @GetMapping("/add/share")
    public Response<AddOrderVO> addShare(@RequestParam Long memberCardId) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.addShare(accountId, memberCardId));
    }


    @ApiOperation(value = "获取分享下单参数")
    @GetMapping("/get/param/share")
    public Response<PrepayWithRequestPaymentResponse> getParamShare(@RequestParam Long id) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.getParamShare(accountId, id));
    }


    @ApiOperation(value = "分享订单回调接口")
    @PostMapping("/notify/share")
    public Response callOrderShare(HttpServletRequest request) {
        Map<String, Object> map = orderService.callOrderShare(request);
        return new Response(0, map.get("code"), map.get("message").toString());
    }


    @ApiOperation(value = "创建微信h5支付")
    @PostMapping("/add/page")
    public Response<com.wechat.pay.java.service.payments.h5.model.PrepayResponse> createPayPage(HttpServletRequest request, @RequestBody PayPageInfoVO payPageInfo) {
        Long accountId = getLoginAccount().getAccountId();
        String remoteAddr = request.getRemoteAddr();
        payPageInfo.setIpInfo(remoteAddr);
        return ResponseFactory.okResponse(orderService.createPayPage(accountId, payPageInfo));
    }


    @ApiOperation(value = "获取订单列表")
    @GetMapping("/get/list")
    public Response<PageVO<OrderVO>> getList(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.getList(accountId, pageable));
    }

    @ApiOperation(value = "获取订单详情")
    @GetMapping("/get")
    public Response<OrderDetailVO> get(@RequestParam Long id) {
        return ResponseFactory.okResponse(orderService.get(id));
    }

    @ApiOperation(value = "添加开票信息")
    @PostMapping("/add/invoice")
    public Response<Boolean> addInvoice(@RequestBody @Validated AddInvoiceForm form) {
        return ResponseFactory.okResponse(orderService.addInvoice(form));
    }

    @ApiOperation(value = "获取开票列表")
    @GetMapping("/get/invoice/list")
    public Response<PageVO<InvoiceVO>> getInvoiceList(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.getInvoiceList(accountId, pageable));
    }

    @ApiOperation(value = "管理端-查询订单列表")
    @PostMapping("/search/page")
    public Response<PageVO<OrderPageVO>> searchPage(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                    @RequestBody @Validated SearchOrderForm form) {
        return ResponseFactory.okResponse(orderService.searchPage(form, pageable));
    }

    @ApiOperation(value = "管理端-更新开票状态")
    @PostMapping("/modify/invoice")
    public Response<Boolean> modifyInvoice(@RequestParam Long id) {
        return ResponseFactory.okResponse(orderService.modifyInvoice(id));
    }

    @ApiOperation(value = "创建订单-app阿里支付")
    @PostMapping("/add/app/ali")
    public Response<String> addByAlipay(@RequestBody @Validated AddOrderForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.addByAlipay(accountId, form));
    }

    @ApiOperation(value = "回调接口-app阿里支付")
    @PostMapping("/notify/app/ali")
    public String callOrderByAlipay(HttpServletRequest request) {
        return orderService.callOrderByAlipay(request);
    }

    @ApiOperation(value = "创建订单-h5阿里支付")
    @GetMapping("/add/app/ali/wap")
    public Response<String> addByAlipayWap(@RequestParam Long memberCardId) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.addByAlipayWap(accountId, memberCardId));
    }

    @ApiOperation(value = "回调接口-h5阿里支付")
    @PostMapping("/notify/app/ali/wap")
    public String callOrderByAlipayWap(HttpServletRequest request) {
        return orderService.callOrderByAlipayWap(request);
    }

    @ApiOperation(value = "创建定时监控订单-h5阿里支付")
    @GetMapping("/add/app/ali/time/wap")
    public Response<String> addByAlipayTimeWap(@RequestParam Long memberCardId, @RequestParam Long monitorRecordId) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.addByAlipayTimeWap(accountId, memberCardId, monitorRecordId));
    }

    @ApiOperation(value = "回调接口-定时监控h5阿里支付")
    @PostMapping("/notify/app/ali/time/wap")
    public String callTimeOrderByAlipayWap(HttpServletRequest request) {
        return orderService.callTimeOrderByAlipayWap(request);
    }


    @ApiOperation(value = "创建订单-支付宝小程序")
    @PostMapping("/add/applet/ali")
    public Response<AddAlipayAppletVO> addByAlipayApplet(@RequestBody @Validated AddOrderByAlipayAppletForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.addByAlipayApplet(accountId, form));
    }

    @ApiOperation(value = "回调接口-支付宝小程序")
    @PostMapping("/notify/applet/ali")
    public String callOrderByAlipayApplet(HttpServletRequest request) {
        return orderService.callOrderByAlipayApplet(request);
    }


    @ApiOperation(value = "获取会员卡显示信息")
    @GetMapping("/get/membership/show")
    public Response<Boolean> getMembershipCardShowInfo() {
        return ResponseFactory.okResponse(orderService.getMembershipCardShowInfo());
    }


    @ApiOperation(value = "获取会员卡订单信息")
    @GetMapping("/get/membership/card")
    public Response<List<MembershipCardSimpleVO>> getMembershipCardOrderInfo(@RequestParam Integer cardType) {
        return ResponseFactory.okResponse(orderService.getMembershipCardOrderInfo(cardType));
    }


    @ApiOperation(value = "创建会员卡订单-支付宝小程序")
    @PostMapping("/add/applet/ali/membership/card")
    public Response<AddAlipayAppletVO> addByAlipayAppletMembershipCard(@RequestBody @Validated AddOrderForMembershipCardForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.addByAlipayAppletMembershipCard(accountId, form));
    }

    @ApiOperation(value = "会员卡回调接口-支付宝小程序")
    @PostMapping("/notify/applet/ali/membership/card")
    public String callOrderByAlipayAppletForMembershipCard(HttpServletRequest request) {
        return orderService.callOrderByAlipayAppletForMembershipCard(request);
    }


    @ApiOperation(value = "创建订单-二维码支付宝")
    @PostMapping("/add/code/ali")
    public Response<String> addByAlipayQrcode(@RequestBody @Validated AddOrderForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(orderService.addByAlipayQrcode(accountId, form));
    }


    @GetMapping("/add/code/ali/test")
    public Response<String> addByAlipayQrcodeTest() {
        return ResponseFactory.okResponse(orderService.addByAlipayQrcodeTest());
    }

    @ApiOperation(value = "回调接口-二维码支付宝")
    @PostMapping("/notify/code/ali")
    public String callOrderByAlipayQrcode(HttpServletRequest request) {
        return orderService.callOrderByAlipayQrcode(request);
    }

    @ApiOperation(value = "测试微信退费")
    @PostMapping("/wx/refund")
    public Refund testWxRefund(@RequestBody RefundOrderDTO data) {
        return wxPayService.executeRefund(data);
    }

    @ApiOperation(value = "测试微信退费查询")
    @GetMapping("/wx/refund/query")
    public Refund testWxRefundQuery(@RequestParam String refundNo) {
        return wxPayService.queryRefund(refundNo);
    }

    @ApiOperation(value = "测试微信商家转账")
    @GetMapping("/wx/transfer")
    public void testWxTransfer() {
        wxPayService.executeTransfer(1L);
    }


}
